package com.tbs.parkingsaleapp.UI.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.tbs.parkingsaleapp.Http.JsonData.OrderInfo;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.UI.Listenners.OrderOpListenner;

import java.sql.Array;
import java.util.ArrayList;

public class OrderAdapter extends BaseAdapter {
    OrderInfo[] infos;
    Context context;
    static ListView ls;
    static OrderAdapter adapter;
    public OrderAdapter(Context context, OrderInfo[] infos, ListView ls)
    {
        adapter=this;
        this.ls=ls;
        this.context=context;
        this.infos=infos;
        for (OrderInfo i:infos
             ) {
            shows.add(i);
        }
    }
    static ArrayList<OrderInfo> shows=new ArrayList<>();

    public static void UpdateItem(OrderInfo ix)
    {
        for(int i=0;i<shows.size();i++)
        {
            if(shows.get(i).getBasic().getId()==ix.getBasic().getId())
            {
                shows.set(i,ix);
                break;
            }
        }
        ls.setAdapter(adapter);
    }

    public static void OnlyFor(int i)
    {
        shows.clear();
        if(i==-2)
        {
            for (OrderInfo ix: adapter.infos
            ) {
                shows.add(ix);
            }
        }
        else
        {
            for (OrderInfo ix: adapter.infos
            ) {
                if(ix.getBasic().getStatus()==i)
                    shows.add(ix);
            }
        }

        ls.setAdapter(adapter);
    }

    @Override
    public int getCount() {
        return shows.size();
    }

    @Override
    public Object getItem(int position) {
        return shows.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OrderInfo info=shows.get(position);
        View root= View.inflate(context, R.layout.singleorder,null);
        root.setTag(info);
        TextView textView=root.findViewById(R.id.order_id);
        textView.setText("订单号："+info.getBasic().getId());
        textView=root.findViewById(R.id.order_time);
        textView.setText("下单时间："+info.getBasic().getTime());
        textView=root.findViewById(R.id.order_location);
        textView.setText("小区地址："+info.getLocationInfo().getAddress());
        textView=root.findViewById(R.id.order_locationname);
        textView.setText("小区名称"+info.getLocationInfo().getName());
        textView=root.findViewById(R.id.order_price);
        textView.setText("价格："+info.getBasic().getPrice());
        textView=root.findViewById(R.id.order_status);
        String status="状态：";
        switch (info.getBasic().getStatus())
        {
            case 0:
                status+="订单审核";
                break;
            case 1:
                status+="订单交易成功";
                break;
            case 2:
                status+="摇号成功";
                break;
            case -1:
                status+="订单关闭";
                break;
        }
        textView.setText(status);
        textView=root.findViewById(R.id.order_parknum);
        textView.setText("车位号:"+info.getBasic().getNumber());
        textView=root.findViewById(R.id.order_comname);
        textView.setText("所属公司:"+info.getCompanyInfo().getName());
        root.setOnLongClickListener(new OrderOpListenner(root.getContext(),info));
        return root;
    }
}
